home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP11 / ABOUT2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  6.1 KB  |  182 lines

  1. /*------------------------------------------
  2.    ABOUT2.C -- About Box Demo Program No. 2
  3.                (c) Charles Petzold, 1996
  4.   ------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "about2.h"
  8.  
  9. LRESULT CALLBACK WndProc      (HWND, UINT, WPARAM, LPARAM) ;
  10. BOOL    CALLBACK AboutDlgProc (HWND, UINT, WPARAM, LPARAM) ;
  11.  
  12. int iCurrentColor  = IDD_BLACK,
  13.     iCurrentFigure = IDD_RECT ;
  14.  
  15. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  16.                     PSTR szCmdLine, int iCmdShow)
  17.      {
  18.      static char  szAppName[] = "About2" ;
  19.      MSG          msg ;
  20.      HWND         hwnd ;
  21.      WNDCLASSEX   wndclass ;
  22.  
  23.      wndclass.cbSize        = sizeof (wndclass) ;
  24.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  25.      wndclass.lpfnWndProc   = WndProc ;
  26.      wndclass.cbClsExtra    = 0 ;
  27.      wndclass.cbWndExtra    = 0 ;
  28.      wndclass.hInstance     = hInstance ;
  29.      wndclass.hIcon         = LoadIcon (hInstance, szAppName) ;
  30.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  31.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  32.      wndclass.lpszMenuName  = szAppName ;
  33.      wndclass.lpszClassName = szAppName ;
  34.      wndclass.hIconSm       = LoadIcon (hInstance, szAppName) ;
  35.  
  36.      RegisterClassEx (&wndclass) ;
  37.  
  38.      hwnd = CreateWindow (szAppName, "About Box Demo Program",
  39.                           WS_OVERLAPPEDWINDOW,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           NULL, NULL, hInstance, NULL) ;
  43.  
  44.      ShowWindow (hwnd, iCmdShow) ;
  45.      UpdateWindow (hwnd) ; 
  46.  
  47.      while (GetMessage (&msg, NULL, 0, 0))
  48.           {
  49.           TranslateMessage (&msg) ;
  50.           DispatchMessage (&msg) ;
  51.           }
  52.      return msg.wParam ;
  53.      }
  54.  
  55. void PaintWindow (HWND hwnd, int iColor, int iFigure)
  56.      {
  57.      static COLORREF crColor[8] = { RGB (0,     0, 0), RGB (  0,   0, 255),
  58.                                     RGB (0,   255, 0), RGB (  0, 255, 255),
  59.                                     RGB (255,   0, 0), RGB (255,   0, 255),
  60.                                     RGB (255, 255, 0), RGB (255, 255, 255) } ;
  61.      HBRUSH          hBrush ;
  62.      HDC             hdc ;
  63.      RECT            rect ;
  64.  
  65.      hdc = GetDC (hwnd) ;
  66.      GetClientRect (hwnd, &rect) ;
  67.      hBrush = CreateSolidBrush (crColor[iColor - IDD_BLACK]) ;
  68.      hBrush = (HBRUSH) SelectObject (hdc, hBrush) ;
  69.  
  70.      if (iFigure == IDD_RECT)
  71.           Rectangle (hdc, rect.left, rect.top, rect.right, rect.bottom) ;
  72.      else
  73.           Ellipse   (hdc, rect.left, rect.top, rect.right, rect.bottom) ;
  74.  
  75.      DeleteObject (SelectObject (hdc, hBrush)) ;
  76.      ReleaseDC (hwnd, hdc) ;
  77.      }
  78.  
  79. void PaintTheBlock (HWND hCtrl, int iColor, int iFigure)
  80.      {
  81.      InvalidateRect (hCtrl, NULL, TRUE) ;
  82.      UpdateWindow (hCtrl) ;
  83.      PaintWindow (hCtrl, iColor, iFigure) ;
  84.      }
  85.  
  86. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  87.      {
  88.      static HINSTANCE hInstance ;
  89.      PAINTSTRUCT      ps ;
  90.  
  91.      switch (iMsg)
  92.           {
  93.           case WM_CREATE :
  94.                hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
  95.                return 0 ;
  96.  
  97.           case WM_COMMAND :
  98.                switch (LOWORD (wParam))
  99.                     {
  100.                     case IDM_ABOUT :
  101.                          if (DialogBox (hInstance, "AboutBox", hwnd,
  102.                                         AboutDlgProc))
  103.                               InvalidateRect (hwnd, NULL, TRUE) ;
  104.                          return 0 ;
  105.                     }
  106.                break ;
  107.  
  108.           case WM_PAINT :
  109.                BeginPaint (hwnd, &ps) ;
  110.                EndPaint (hwnd, &ps) ;
  111.  
  112.                PaintWindow (hwnd, iCurrentColor, iCurrentFigure) ;
  113.                return 0 ;
  114.                     
  115.           case WM_DESTROY :
  116.                PostQuitMessage (0) ;
  117.                return 0 ;
  118.           }
  119.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  120.      }
  121.  
  122. BOOL CALLBACK AboutDlgProc (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
  123.      {
  124.      static HWND hCtrlBlock ;
  125.      static int  iColor, iFigure ;
  126.  
  127.      switch (iMsg)
  128.           {
  129.           case WM_INITDIALOG :
  130.                iColor  = iCurrentColor ;
  131.                iFigure = iCurrentFigure ;
  132.  
  133.                CheckRadioButton (hDlg, IDD_BLACK, IDD_WHITE, iColor) ;
  134.                CheckRadioButton (hDlg, IDD_RECT,  IDD_ELL,   iFigure) ;
  135.  
  136.                hCtrlBlock = GetDlgItem (hDlg, IDD_PAINT) ;
  137.  
  138.                SetFocus (GetDlgItem (hDlg, iColor)) ;
  139.                return FALSE ;
  140.  
  141.           case WM_COMMAND :
  142.                switch (LOWORD (wParam))
  143.                     {
  144.                     case IDOK :
  145.                          iCurrentColor  = iColor ;
  146.                          iCurrentFigure = iFigure ;
  147.                          EndDialog (hDlg, TRUE) ;
  148.                          return TRUE ;
  149.  
  150.                     case IDCANCEL :
  151.                          EndDialog (hDlg, FALSE) ;
  152.                          return TRUE ;
  153.  
  154.                     case IDD_BLACK :
  155.                     case IDD_RED :
  156.                     case IDD_GREEN :
  157.                     case IDD_YELLOW :
  158.                     case IDD_BLUE :
  159.                     case IDD_MAGENTA :
  160.                     case IDD_CYAN :
  161.                     case IDD_WHITE :
  162.                          iColor = LOWORD (wParam) ;
  163.                          CheckRadioButton (hDlg, IDD_BLACK, IDD_WHITE, LOWORD (wParam)) ;
  164.                          PaintTheBlock (hCtrlBlock, iColor, iFigure) ;
  165.                          return TRUE ;
  166.  
  167.                     case IDD_RECT :
  168.                     case IDD_ELL :
  169.                          iFigure = LOWORD (wParam) ;
  170.                          CheckRadioButton (hDlg, IDD_RECT, IDD_ELL, LOWORD (wParam)) ;
  171.                          PaintTheBlock (hCtrlBlock, iColor, iFigure) ;
  172.                          return TRUE ;
  173.                     }
  174.                break ;
  175.  
  176.           case WM_PAINT :
  177.                PaintTheBlock (hCtrlBlock, iColor, iFigure) ;
  178.                break ;
  179.           }
  180.      return FALSE ;
  181.      }
  182.